
#include <drawables/sprite.hpp>
#include "assert.h"
#include <window/window.hpp>


bool sprite::draw( float x, float y, float rotation, float scale )
{
	m_sprite.SetX(m_position.x+x);
	m_sprite.SetY(m_position.y+y);
	m_sprite.SetRotation(m_rotation+rotation);
	m_sprite.SetScale(m_scale*scale, m_scale*scale);

	if( m_flip_horizontal )
		m_sprite.FlipX(true);
	else
		m_sprite.FlipX(false);

	m_sprite.SetSubRect( m_sub_rect );

	g_window->Draw(m_sprite);

	return true;
}

sprite::sprite( const std::string & filepath, float x, float y, float rot, float scale )
{
	m_position.x = x;
	m_position.y = y;
	m_rotation = rot;
	m_scale = scale;

	sf::Image * image = g_cache.load_image( filepath );
	assert( image != 0 && "image load... FAIL" );

	m_sprite.SetImage(*image);
	m_sub_rect = m_sprite.GetSubRect();
}

bool sprite::fill( const std::string & filepath, float x, float y, float rot, float scale )
{
	m_position.x = x;
	m_position.y = y;
	m_rotation = rot;
	m_scale = scale;
	sf::Image * image = g_cache.load_image( filepath );
	if( image == 0 )
		return false;

	m_sprite.SetImage(*image);
	m_sub_rect = m_sprite.GetSubRect();

	return true;
}